package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.ScenicSpot;
import cn.wolfcode.trip.base.domain.Theme;
import cn.wolfcode.trip.base.domain.Ticket;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IScenicSpotService;
import cn.wolfcode.trip.base.service.ITicketService;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/scenicSpot")
//景点管理控制器
public class ScenicSpotController {

    @Autowired
    private IScenicSpotService scenicSpotService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private ITicketService ticketService;

    //查询所有的景点以及所在地区
    @RequestMapping("/list")
    public String list(@Param("qo") QueryObject qo, Model model){
        PageInfo<ScenicSpot> pageInfo= scenicSpotService.query(qo);
        //查询所有的地区(应该查询市级地区 还不会弄)
        List<Region> regions=regionService.list(null);
        List<Theme> themes=scenicSpotService.listTheme();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("regions",regions);
        model.addAttribute("themes",themes);
        return "/scenicSpot/list";
    }
//保存景点资料
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(ScenicSpot entity, MultipartFile img){
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            entity.setCoverUrl(url);
        }
        scenicSpotService.saveOrUpdate(entity);

        return "redirect:/scenicSpot/list.do";
    }



}
